package com.edu.whu.controller;

import com.edu.whu.entity.Content;
import com.edu.whu.service.ContentService;
import com.edu.whu.service.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/content")
public class ContentController {
    @Autowired
    private ContentService contentService;

    @GetMapping
    public List<Content> getAllContent() {
        return contentService.getAllContent();
    }

    @GetMapping("/{id}")
    public Content getContent(@PathVariable int id) throws ServiceException {
        return contentService.getContent(id);
    }

    @PostMapping
    public Content postContent(@RequestHeader(HttpHeaders.AUTHORIZATION) String tokenWrapper,  @RequestBody Content content) throws ServiceException {
        String token = tokenWrapper.substring(6);
        return contentService.postContent(token, content);
    }

    @DeleteMapping("/{id}")
    public void deleteContent(@RequestHeader(HttpHeaders.AUTHORIZATION) String tokenWrapper, @PathVariable int id) throws ServiceException {
        String token = tokenWrapper.substring(6);
        contentService.deleteContent(token, id);
    }
}
